package com.lxmes.hr.service.impl;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.*;
import com.lxmes.hr.mapper.NcPersonnelInfoMapper;
import com.lxmes.hr.mapper.NcPieceworkStaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcPieceworkParamMapper;
import com.lxmes.hr.service.INcPieceworkParamService;

import javax.validation.Validator;

/**
 * 计件参数Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-16
 */
@Service
public class NcPieceworkParamServiceImpl implements INcPieceworkParamService
{
    @Autowired
    private NcPieceworkParamMapper ncPieceworkParamMapper;

    @Autowired
    private NcPieceworkStaMapper ncPieceworkStaMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询计件参数
     *
     * @param id 计件参数主键
     * @return 计件参数
     */
    @Override
    public NcPieceworkParam selectNcPieceworkParamById(Long id)
    {
        return ncPieceworkParamMapper.selectNcPieceworkParamById(id);
    }

    /**
     * 查询计件参数列表
     *
     * @param ncPieceworkParam 计件参数
     * @return 计件参数
     */
    @Override
    public List<NcPieceworkParam> selectNcPieceworkParamList(NcPieceworkParam ncPieceworkParam)
    {
        return ncPieceworkParamMapper.selectNcPieceworkParamList(ncPieceworkParam);
    }

    /**
     * 新增计件参数
     *
     * @param ncPieceworkParam 计件参数
     * @return 结果
     */
    @Override
    public int insertNcPieceworkParam(NcPieceworkParam ncPieceworkParam)
    {
        ncPieceworkParam.setCreateTime(DateUtils.getNowDate());
        return ncPieceworkParamMapper.insertNcPieceworkParam(ncPieceworkParam);
    }

    /**
     * 修改计件参数
     *
     * @param ncPieceworkParam 计件参数
     * @return 结果
     */
    @Override
    public int updateNcPieceworkParam(NcPieceworkParam ncPieceworkParam)
    {
        ncPieceworkParam.setUpdateTime(DateUtils.getNowDate());
        return ncPieceworkParamMapper.updateNcPieceworkParam(ncPieceworkParam);
    }

    /**
     * 批量删除计件参数
     *
     * @param ids 需要删除的计件参数主键
     * @return 结果
     */
    @Override
    public int deleteNcPieceworkParamByIds(Long[] ids)
    {
        return ncPieceworkParamMapper.deleteNcPieceworkParamByIds(ids);
    }

    /**
     * 删除计件参数信息
     *
     * @param id 计件参数主键
     * @return 结果
     */
    @Override
    public int deleteNcPieceworkParamById(Long id)
    {
        return ncPieceworkParamMapper.deleteNcPieceworkParamById(id);
    }

    @Override
    public String imporIteminfo(List<NcPieceworkParam> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入物料数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcPieceworkParam org : userList)
        {
            try
            {
                BeanValidators.validateWithException(validator, org);
                org.setCreateBy(operName);
                org.setUpdateBy(operName);
                this.insertNcPieceworkParam(org);
                successNum++;
                successMsg.append("<br/>" + successNum + "、标准 " + org.getName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + org.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int readPersonnelInfo() {
        NcPieceworkSta ncPieceworkSta = new NcPieceworkSta();
        ncPieceworkSta.setIsRead("0");
        List<NcPieceworkSta> ncPieceworkStas = ncPieceworkStaMapper.selectNcPieceworkStaList(ncPieceworkSta);
        if(ncPieceworkStas.size()!=0){
            ncPieceworkStas.parallelStream().forEach(p->{
                NcPieceworkParam ncPieceworkParam = new NcPieceworkParam();
                ncPieceworkParam.setItemCode(p.getItemCode());
                List<NcPieceworkParam> infos = ncPieceworkParamMapper.selectNcPieceworkParamList(ncPieceworkParam);
                ncPieceworkParam.setGcCode(p.getGcCode());
                ncPieceworkParam.setGcName(p.getGcName());
                ncPieceworkParam.setName(p.getName());
                ncPieceworkParam.setDepartment(p.getDepartment());
                ncPieceworkParam.setBranch(p.getBranch());
                ncPieceworkParam.setPGroup(p.getPGroup());
                ncPieceworkParam.setJobs(p.getJobs());
                ncPieceworkParam.setCj(p.getCj());
                ncPieceworkParam.setProLine(p.getProLine());
                ncPieceworkParam.setGwCode(p.getGwCode());
                ncPieceworkParam.setGwName(p.getGwName());
                ncPieceworkParam.setItemCode(p.getItemCode());
                ncPieceworkParam.setItemName(p.getItemName());
                ncPieceworkParam.setItemSubCode(p.getItemCode());
                if(infos.size()==0){
                    ncPieceworkParamMapper.insertNcPieceworkParam(ncPieceworkParam);
                    ncPieceworkSta.setIsRead("1");
                    ncPieceworkSta.setId(p.getId());
                    ncPieceworkStaMapper.updateNcPieceworkSta(ncPieceworkSta);
                }
            });
        }
        return 1;
    }
}
